﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

namespace AUV5.Web.Service
{
    public partial class ServiceFeedBackPagedList : PageBaseService
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int pageindex = 0;
            int pagesize = 0;
            try
            {
                pageindex = int.Parse(Request["pageindex"]??"0");
                pagesize = int.Parse(Request["pagesize"]??"0");
            }
            catch
            {
                Response.Write("113");//无效的整型参数
            } 
            //服务ID
            string serviceid = Request["serviceid"] ?? "";

            //用户ID
            string userid = Request["userid"] ?? "";

            string secretkey=Request["secretkey"]??"";
            if (!IsValidKey(secretkey, pageindex, pagesize, serviceid, userid))
            {
                Response.Write("112");//密钥不正确 
                return;
            }


            if (Request["pageindex"] != null && Request["pagesize"] != null)
            {
                
                string sqlwhere = string.Format("State={0} and IsDel={1}",(int)Common.Enum.auvState.Enabled,(int)Common.Enum.auvDel.NotDel);
                string orderby = "CreateDate Desc,FeedBackID asc";
                int recordcount = 0;
                if (!string.IsNullOrEmpty(serviceid))
                {
                    if (BLL.ServiceInfoBLL.Instance.GetDetail(serviceid) != null)
                    {
                        sqlwhere += " and ServiceID='" + serviceid + "'";
                    }
                    else
                    {
                        Response.Write("300");//服务编号无效
                    }
                }

                
                if (!string.IsNullOrEmpty(userid))
                {
                    if (BLL.UserInfoBLL.Instance.GetDetail(userid) != null)
                    {
                        sqlwhere += " and UserID='" + userid + "'";
                    }
                    else
                    {
                        Response.Write("301");//用户编号无效
                    }
                }
                IList<Model.FeedBack> list = BLL.FeedBackBLL.Instance.GetPagedFeedBack(pageindex, pagesize, sqlwhere, orderby, ref recordcount);
                Hashtable ht = new Hashtable();
                ht.Add("RecordCount", recordcount);
                ht.Add("FeedBackList", list);

                Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(ht));
            }
            else
            {
                Response.Write("111");//参数不正确
            }
        }

        private bool IsValidKey(string secretkey, int pageindex, int pagesize, string serviceid, string userid)
        {
            if (secretkey_enable)
            {
                if (string.IsNullOrEmpty(secretkey))
                {
                    return false;
                }
                else
                {
                    string finalkey = base.EncryptMD5(public_key + current_day + (pageindex * pagesize).ToString() + serviceid + userid).Substring(6, 22);
                    return secretkey == finalkey;
                }
            }
            else
            {
                return true;
            }
        }
    }
}